package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.EnableNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;


    @Override
    public void insertSetmeal(SetmealDTO setmealDTO) {

        //新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insert(setmeal);
        //新增套餐 菜品关联表
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //需要给每一个关联数据设置 套餐id
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
            setmealDishMapper.insertBatch(setmealDishes);
        }


    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);
        Page<SetmealVO> pageResult = setmealMapper.selectByCondition(setmeal);

        return new PageResult(pageResult.getTotal(), pageResult.getResult());
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            return;
        }

        int enableCount = setmealMapper.countEnableByIds(ids);

        if (enableCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //批量删除
        setmealMapper.deleteByIds(ids);
        //批量删除 套餐菜品关联数据
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐 需要注意 所有套餐内的菜品必须是起售状态
        if (status == StatusConstant.ENABLE) {
            int count = setmealDishMapper.countDiableDishesBySetmealId(id);
            if (count > 0) {
                throw new EnableNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
    }

    @Override
    public SetmealVO selectById(Long id) {

        Setmeal setmeal = setmealMapper.selectById(id);


        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(setmeal.getId());

        setmealDishMapper.deleteBySetmealIds(ids);

        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
            setmealDishMapper.insertBatch(setmealDishes);
        }


    }

    @Override
    public List<SetmealVO> list(Setmeal setmeal) {
        List<SetmealVO> setmealVOList = setmealMapper.selectByCondition(setmeal);

        return setmealVOList;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        //查询套餐内部的菜品 的 名  分数  图  描述
        List<DishItemVO> list= setmealDishMapper.selectDishVOBySetmealId(id);
        return list;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE);
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE);
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder().sold(sold).discontinued(discontinued).build();



        return setmealOverViewVO;
    }
}
